setwd("../../") ## replication directory
(rep.dir <- getwd())

load("output-data/subsampled/subsampled_output.RData")

library(foreign) ## for read.dta
library(dplyr) ## for %>%, group_by, etc.
library(plm) ## for panel models
library(stargazer) ## for nice latex tables
library(rms) ## for robust SEs
library(lmtest) ## for coeftest
library(clusterSEs) ## for bootstrapped SEs
library(reshape2) ## for melt
library(car)
library(ggplot2)
library(reshape2)
library(maps)
library(mapproj)

StarsToBold <- function (text) {
  out <- gsub("([[:digit:]]+)\\^\\{\\*+\\}", "\\\\textbf{\\1}", text)
  cat(out, sep="\n")
  invisible(out)
}

### TABLE 1
tab1.mods %>%
  stargazer(
    type="latex"
  , float=FALSE
  , se=tab1.moc.se
  , coef=tab1.moc.est
  , t.auto=TRUE
  , p.auto=TRUE ## from standard normal (very close to t when DF = 3,500)
  , omit=c("Year|Constant|^South11South$", "StPO")
  , omit.labels=c("Year $\\times$ South FEs", "State FEs")
  , omit.stat=c("F", "rsq")
  , omit.table.layout = "n"
  , align=TRUE
  , initial.zero=FALSE
  , dep.var.caption="DV: Domain-Specific Policy Liberalism ($t$)"
  , dep.var.labels=c(
      "------------ Social ------------ $|$ ----------- Economic -----------")
  , dep.var.labels.include=TRUE
  , column.labels=c("XS", "FE", "LDV", "DP", "XS", "FE", "LDV", "DP")
  , order=c("MassM1\\_mean$", "MassM1\\_mean:South11South",
            "PolicyM1")
  , no.space=TRUE
  , digits=3
  , column.sep.width = "1pt"
  , covariate.labels=c(
      "Mass Liberalism$_{t-1}$",
      "Mass Lib$_{t-1}$ $\\times$ South",
      "Policy Liberalism$_{t-1}$")
  ) %>%
  StarsToBold()

tab2.mods %>%
  stargazer(
    type="latex"
  , float=FALSE
  , se=tab2.moc.se
  , coef=tab2.moc.est
  , t.auto=TRUE
  , p.auto=TRUE ## from standard normal (very close to t when DF = 3,500)
  , omit=c("Year", "StPO|Constant")
  , omit.labels=c("Year FEs", "State FEs")
  , omit.stat=c("F", "rsq")
  , omit.table.layout = "n"
  , initial.zero=FALSE
  , align=TRUE
  , dep.var.caption="DV: Democratic Control Index  ($t$)"
  , dep.var.labels.include=FALSE
  , no.space=TRUE
  , digits=3
  , column.sep.width = "1pt"
  , order=c("MassSocial", "MassEcon", "PID", "Control")
  , covariate.labels=c(
      "Mass Social Lib$_{t-1}$",
      "Mass Econ Lib$_{t-1}$",
      "Mass Dem PID$_{t-2}$",
      "Dem Control$_{t-1}$"
      )
  ) %>%
  StarsToBold()

tab3.mods %>%
  stargazer(
    type="latex"
  , float=FALSE
  , se=tab3.moc.se
  , coef=tab3.moc.est
  , t.auto=TRUE
  , p.auto=TRUE ## from standard normal (very close to t when DF = 3,500)
  , omit=c("^Year", "StPO|Constant")
  , omit.labels=c("Year FEs", "State FEs")
  , omit.stat=c("F", "rsq")
  , omit.table.layout = "n"
  , align=TRUE
  , dep.var.caption="DV: Domain-Specific Policy Liberalism ($t$)"
  , dep.var.labels=c(
      "-------------- Social --------------- $|$ ----------- Economic -----------")
  , dep.var.labels.include=TRUE
  , order=c("DemControl", "MassM1\\_mean$", "YearAfterHouseElectionFALSE",
            "YearAfterHouseElectionTRUE", "Policy")
  , single.row=FALSE
  , no.space=TRUE
  , initial.zero=FALSE
  , digits=3
  , column.sep.width = "-10pt"
  , covariate.labels=c(
      "Dem Control$_{t}$",
      "Mass Lib$_{t-1}$",
      "Mass Lib$_{t-1}$ (No Elec$_{t-1}$)",
      "Mass Lib$_{t-1}$ (Elec$_{t-1}$)",
      "Policy Lib$_{t-1}$")    
  ) %>%
  StarsToBold()

tab4.mods %>%
  stargazer(
    type="latex"
  , float=FALSE
  , se=tab4.moc.se
  , coef=tab4.moc.est
  , t.auto=TRUE
  , p.auto=TRUE ## from standard normal (very close to t when DF = 4,500)
  , omit=c("^Year|^Pre72Pre-1972$|^Pre72Pre-1972:South11South$",
           "StPO|Constant|^South11South$")
  , omit.labels=c("Year FEs", "State FEs")
  , omit.stat=c("F", "rsq")
  , omit.table.layout = "n"
  , align=TRUE
  , dep.var.caption="DV: Domain-Specific Policy Liberalism ($t$)"
  , dep.var.labels=c(
      "-------------- Social -------------- $|$ ----------- Economic -----------")
  , dep.var.labels.include=TRUE
  , single.row=FALSE
  , no.space=TRUE
  , initial.zero=FALSE
  , digits=3
  , column.sep.width = "-10pt"
  , order=c("^MassM1\\_mean$",
            "^MassM1\\_mean:Pre72Pre-1972$",
            "^MassM1\\_mean:South11South$",
            "Pre72Pre-1972:South11South$",
            "Suffrage", "Campaign", "Citizen", "LegDays", "PolicyM1")
  , covariate.labels=c(
      "Mass Liberalism$_{t-1}$",
      "Mass Lib$_{t-1} \\times$ Pre-1972",
      "Mass Lib$_{t-1} \\times$ South",
      "Mass Lib$_{t-1} \\times$ Pre-1972 $\\times$ South",
      "Suffrage Restriction",
      "Suff Restrict $\\times$ Mass Lib$_{t-1}$",
      "Contribution Limits",
      "Contrib Limit $\\times$ Mass Lib$_{t-1}$",
      "Citizen Government",
      "Citizen Gov't $\\times$ Mass Lib$_{t-1}$",
      "Legislative Days (Logged)",
      "Leg Days $\\times$ Mass Lib$_{t-1}$",
      "Policy Liberalism$_{t-1}$")
  ) %>%
  StarsToBold()
